﻿using System;

namespace Program19
{
    class Program
    {
        static void Main(string[] args)
        {
            //1.用方法来实现：有一个字符串数组：{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。
            //2.用方法来实现：请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }。要求：计算结果如果有小数，则显示小数点后两位（四舍五入）。
            //3.请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。 
            string[] strs = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
            string max = GetMaxLength(strs);
            Console.WriteLine(max);

            int[] nums = { 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 };
            double avg = GetAvg(nums);
            Console.WriteLine("{0:0.00}", avg);

            int[] arr = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
            GetSort(arr);
            //Console.WriteLine("排序后：");
            //foreach (var item in arr)
            //{
            //    Console.Write(item + " ");
            //}
            //Console.ReadLine();
        }
        /// <summary>
        /// 求最长的字符串
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public static string GetMaxLength(string[] items)
        {
            int str = items[0].Length;
            string tmp = "";
            for (int i = 0; i < items.Length; i++)
            {
                if (str < items[i].Length)
                {
                    tmp = items[i];
                }
            }
            return tmp;
        }

        /// <summary>
        /// 求平均值
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static double GetAvg(int[] item)
        {
            int sum = 0;
            for (int i = 0; i < item.Length; i++)
            {
                sum += item[i];
            }
            return sum / item.Length;
        }

        /// <summary>
        /// 冒泡排序
        /// </summary>
        /// <param name="item"></param>
        public static void GetSort(int[] item)
        {
            Console.WriteLine("排序前：");
            for(var i =0;i<item.Length;i++)
            {
                Console.Write(item[i] + " ");
            }
            Console.WriteLine();
            Console.WriteLine("排序后：");
            for (int i = 1; i < item.Length; i++)
            {
                for (int j = 0; j < item.Length - i; j++)
                {
                    if (item[j] > item[j + 1])
                    {
                        int tmp = item[j];
                        item[j] = item[j + 1];
                        item[j + 1] = tmp;
                    }
                }
            }
            for (var i = 0; i < item.Length; i++)
            {
                Console.Write(item[i] + " ");
            }
            Console.ReadLine();
        }
    }
}
